package com.grass.cropTradingCenter.handler;

import com.grass.cropTradingCenter.common.BusinessException;
import com.grass.cropTradingCenter.common.OutputException;
import com.grass.cropTradingCenter.common.Result;
import com.grass.cropTradingCenter.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(OutputException.class)
    public Result outputExceptionHandler(OutputException e){
        log.error("OutputException:" + e.getMessage(), e);
        return Result.error(e);
    }

    @ExceptionHandler(BusinessException.class)
    public Result businessExceptionHandler(BusinessException e){
        log.error("BusinessException:" + e.getMessage(), e);
        return Result.error(e);
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeException(RuntimeException e){
        log.error("RuntimeException:" + e.getMessage(), e);
        return new Result(ResultCode.SYSTEM_ERROR, e.getMessage());
    }
}
